package com.sheyu.mall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.sheyu.mall.order.entity.OmsOrderEntity;
import com.sheyu.mall.order.entity.OmsOrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.order.dao.OmsOrderItemDao;
import com.sheyu.mall.order.entity.OmsOrderItemEntity;
import com.sheyu.mall.order.service.OmsOrderItemService;

@RabbitListener(queues = "shuying-queue")
@Service("omsOrderItemService")
public class OmsOrderItemServiceImpl extends ServiceImpl<OmsOrderItemDao, OmsOrderItemEntity> implements OmsOrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OmsOrderItemEntity> page = this.page(
                new Query<OmsOrderItemEntity>().getPage(params),
                new QueryWrapper<OmsOrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param message
     * @param orderReturnReasonEntity
     * @param channel 当前传输的通道
     */
    @RabbitHandler
    public void recieveMessage(Message message, OmsOrderReturnReasonEntity orderReturnReasonEntity,
                               Channel channel){
        System.out.println("接受消息。。。内容,"+message+"==》类型"+orderReturnReasonEntity);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            channel.basicAck(deliveryTag,false);  //false就不是批处理
        } catch (IOException e) {
            try {
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
    @RabbitHandler
    public void recieveMessage1(Message message, OmsOrderEntity orderEntity,Channel channel){

        System.out.println("接受消息"+message+"类型"+orderEntity);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            channel.basicAck(deliveryTag,false);  //false就不是批处理
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
